package com.hz.qs.service;

import com.hz.qs.dao.DoctorDao;
import com.hz.qs.model.YiCallPatient;
import com.hz.qs.model.YiQueues;
import com.hz.qs.model.vo.CustomerDataVo;
import com.hz.qs.utils.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import static com.hz.qs.utils.Common.*;

@Service
public class DoctorService {

    @Autowired
    private DoctorDao doctorDao;


    public RestResult getCustomerDataByDept(String currentDate, String queueId, String customerName) {
        String firstDate = currentDate + START_TIME;
        String endDate = currentDate + END_TIME;
        List<CustomerDataVo> list = doctorDao.getCustomerDataByDept(firstDate, endDate, queueId, customerName);
        List<CustomerDataVo> sortList = list.stream().sorted(Comparator.comparing(CustomerDataVo::getOrderNo)).collect(Collectors.toList());
        return RestResult.successResult(sortList);
    }

    /**
     * 医生叫号
     *
     * @param callPatient
     * @return java.lang.Integer
     * @author yejy
     * @date 2020/11/26 14:18
     */
    @Transactional(rollbackFor = Exception.class)
    public Integer callPatient(YiCallPatient callPatient) {
        LinkedList<YiQueues> queuesByCustomerId = doctorDao.selectYiQueuesByCustomerId_QueueId(callPatient.getCustomerId(), callPatient.getQueueId());
        if (queuesByCustomerId.get(0).getState().equals(QUEUE_STATE_QIAN_DAO)) {
            callPatient.setCallState(CALL_ING);
            callPatient.setCallDate(new Date());
            return doctorDao.insertSelective(callPatient);
        } else {
            return -1;
        }
    }

    public RestResult docLoopTips(String customerId, String currentDate) {
        String sDate = currentDate + START_TIME;
        String eDate = currentDate + END_TIME;
        List<YiCallPatient> list = doctorDao.docLoopTips(customerId, sDate, eDate, JGS_CONFIRM);
        return RestResult.successResult(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public RestResult docCancelCall(String id) {
        YiCallPatient yiCallPatient = doctorDao.selectByPrimaryKey(id);
        Integer callState = yiCallPatient.getCallState();
        if (callState.equals(CALL_ING)) {
            YiCallPatient callPatient = new YiCallPatient();
            callPatient.setId(id);
            callPatient.setCallState(DOC_CANCEL);
            doctorDao.updateByPrimaryKeySelective(callPatient);
            return RestResult.successResult(null);
        } else if (callState.equals(JGS_CONFIRM)) {
            return RestResult.failResult("健管师确认叫号！！！不允许再取消叫号！！", 4001);
        } else if (callState.equals(DOC_CONFIRM)) {
            return RestResult.successResult("医生已经确认，请尽快就诊！！");
        }
        return RestResult.failResult("异常，请和管理员联系！！！" + id, 500);
    }

    public RestResult getQueueByDeptCode(String deptCode) {
        List<YiQueues> list = doctorDao.getQueueByDeptCode(deptCode);
        return RestResult.successResult(list);
    }

    @Transactional(rollbackFor = Exception.class)
    public RestResult docFinish(String checkId, String projectId, String docName) {
        doctorDao.docFinish(checkId, projectId, docName);
        LinkedList<YiQueues> queuesLinkedList = doctorDao.selectYiQueuesByCustomerId_QueueId(checkId, "");
        boolean allFive = queuesLinkedList.stream().allMatch(f -> f.getState().equals(QUEUE_STATE_JIAN_CHA));
        if (allFive) {
            doctorDao.docFinish_st(checkId, projectId);
        }
        return RestResult.successResult(null);
    }
}
